package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.controller.RbacRoleController;
import com.woniuxy.dao.RbacRoleDao;
import com.woniuxy.entity.RbacMenu;
import com.woniuxy.entity.RbacRole;
import com.woniuxy.entity.RbacRoleMenuDTO;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.RbacRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class RbacRoleServiceImpl implements RbacRoleService {
    @Autowired
    RbacRoleDao rbacRoleDao;
    @Override
    public String findByRbacManager(String username) {
        return rbacRoleDao.findByRbacManager(username);
    }

    @Override
    public ResponseEntity findAll(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<RbacRole> rbacRoleList=rbacRoleDao.findAll();
        PageInfo pageInfo=new PageInfo(rbacRoleList);
        return new ResponseEntity(200,"执行成功",pageInfo);
    }

    @Override
    public ResponseEntity checkedRole(int roleId) {
        List<Integer> checked = rbacRoleDao.checkedRole(roleId);
        return new ResponseEntity(200,"执行成功",checked);
    }

    @Override
    @Transactional
    public ResponseEntity changeMenus(RbacRoleMenuDTO rbacRoleMenuDTO) {
        //考虑下如何保存数据？先删除初始数据在全部新增进去
        int i=rbacRoleDao.delete(rbacRoleMenuDTO.getRoleid());
        int j =rbacRoleDao.saveChangeMenus(rbacRoleMenuDTO.getRoleid(),rbacRoleMenuDTO.getModuleIds());
        if(j>0){
            return new ResponseEntity(200,"成功更新权限菜单数据",null);
        }
        return ResponseEntity.FAIL;
    }


}
